package com.easyjob.controller;

import com.alibaba.fastjson.JSONObject;
import com.easyjob.annotation.GlobalInterceptor;
import com.easyjob.annotation.VerifyParam;
import com.easyjob.entity.enums.PostStatusEnum;
import com.easyjob.entity.enums.RequestFrequencyTypeEnum;
import com.easyjob.entity.enums.ResponseCodeEnum;
import com.easyjob.entity.po.*;
import com.easyjob.entity.query.*;
import com.easyjob.entity.vo.*;
import com.easyjob.exception.BusinessException;
import com.easyjob.service.*;
import netscape.javascript.JSObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类 Controller
 */
@RestController("indexController")
@RequestMapping("/index")
public class IndexController extends ABaseController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private AppCarouselService appCarouselService;

    @Resource
    private ExamQuestionService examQuestionService;

    @Resource
    private ExamQuestionItemService examQuestionItemService;

    @Resource
    private AppDeviceService appDeviceService;

    @Resource
    private QuestionBankService questionBankService;


    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadAllCategory")
    @GlobalInterceptor
    public ResponseVO loadAllCategory() {
        List<Category> categoryList = categoryService.loadAllCategory();
        List<JSONObject> list = categoryList.stream().map(item -> {
            JSONObject object = new JSONObject();
            object.put("text", item.getCategoryName());
            object.put("value", item.getCategoryId());
            return object;
        }).collect(Collectors.toList());
        return getSuccessResponseVO(list);
    }

    @RequestMapping("/loadCarousel")
    @GlobalInterceptor
    public ResponseVO loadCarousel() {
        AppCarouselQuery query = new AppCarouselQuery();
        query.setOrderBy("sort asc");
        List<AppCarousel> carouselList = appCarouselService.findListByParam(query);
        return getSuccessResponseVO(carouselList);
    }

    @RequestMapping("/loadTabs")
    @GlobalInterceptor
    public ResponseVO loadTabs() {
        QuestionBankQuery query = new QuestionBankQuery();
        query.setOrderBy("sort asc");
        List<QuestionBank> bankList = questionBankService.findListByParam(query);
        return getSuccessResponseVO(bankList);
    }
    @RequestMapping("/loadCategoryList")
    @GlobalInterceptor
    public ResponseVO loadCategoryList(@RequestParam(name = "id", defaultValue = "10014") Integer bankId) {
        List<CategoryInfoVo> categoryInfoVoList = categoryService.loadAllCategoryInfo(bankId);
        return getSuccessResponseVO(categoryInfoVoList);
    }
    @RequestMapping("/loadCategory")
    @GlobalInterceptor
    public ResponseVO loadCategory(@RequestParam(name = "id", defaultValue = "10014") Integer bankId) {
        List<CategoryVo> categoryVoList = categoryService.loadCategory(bankId);
        return getSuccessResponseVO(categoryVoList);
    }

    @RequestMapping("/loadChapter")
    @GlobalInterceptor
    public ResponseVO loadChapter(@RequestParam(name = "id") Integer categoryId) {
        List<ChapterVo> categoryDtoList = categoryService.loadChapter(categoryId);
        return getSuccessResponseVO(categoryDtoList);
    }

    @RequestMapping("/loadModels")
    @GlobalInterceptor
    public ResponseVO loadModels() {
        List<ModelVo> modelVoList = categoryService.loadModels();
        return getSuccessResponseVO(modelVoList);
    }


    @RequestMapping("/getExamQuestionById")
    @GlobalInterceptor
    public ResponseVO getExamQuestionById(@VerifyParam(required = true) Integer questionId) {
        ExamQuestion examQuestion = examQuestionService.getExamQuestionByQuestionId(questionId);
        if (examQuestion == null || !PostStatusEnum.POST.getStatus().equals(examQuestion.getStatus())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        ExamQuestionItemQuery itemQuery = new ExamQuestionItemQuery();
        itemQuery.setQuestionId(examQuestion.getQuestionId());
        itemQuery.setOrderBy("sort asc");
        List<ExamQuestionItem> questionItemList = examQuestionItemService.findListByParam(itemQuery);
        examQuestion.setQuestionItemList(questionItemList);
        return getSuccessResponseVO(examQuestion);
    }

    @RequestMapping("/report")
    @GlobalInterceptor(checkParams = false, frequencyType = RequestFrequencyTypeEnum.DAY, reqeustFrequencyThreshold = 10)
    public ResponseVO report(HttpServletRequest request,
                             @VerifyParam(required = true, max = 32) String deviceId,
                             @VerifyParam(max = 30) String deviceBrand) {
        AppDevice appDevice = new AppDevice();
        appDevice.setDeviceId(deviceId);
        appDevice.setDeviceBrand(deviceBrand);
        appDevice.setIp(getIpAddr(request));
        appDeviceService.reportData(appDevice);
        return getSuccessResponseVO(null);
    }
}